package org.zfes.snowier.cloud.zbss.layer.tc.biz.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zfes.snowier.core.data.DataSet;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZStrUtil;
import org.zfes.snowier.web.annos.ListParam;
import org.zfes.snowier.web.controller.BaseController;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.auth.SessionUser;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.consts.BizConst;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.model.Percentage;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.model.PercentageExport;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.service.IPercentageService;
import org.zfes.snowier.cloud.zbss.layer.tc.biz.service.impl.ExportServiceImpl;
@Controller 
@RequestMapping("/tcms/percentage")
public class PercentageController extends BaseController {

	private String viewPathPrefix="/tcms";
	
	@Autowired
	private IPercentageService percentageService;
	@Autowired
	private ExportServiceImpl exportServiceImpl;
	
	
	@RequestMapping(value={"/",""}, method = RequestMethod.GET,produces = MediaType.TEXT_HTML_VALUE)
    public Object index(){
		return forward(viewPathPrefix,"percentage.html");
    }
    @RequestMapping(value="/doEexport", method = {RequestMethod.GET,RequestMethod.POST})
    public void doEexport(HttpServletRequest httpRequest,@RequestParam Map<String,Object> params,HttpServletResponse response) throws IOException{
    	 try {
    	
    	 response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode("projects.xlsx", "UTF-8"));
         response.setContentType("application/vnd.ms-excel;charset=UTF-8");
         response.setHeader("Pragma", "no-cache");
         response.setHeader("Cache-Control", "no-cache");
         response.setDateHeader("Expires", 0);
         SessionUser su= (SessionUser) httpRequest.getSession().getAttribute(BizConst.SESSION_USER);
     	if(su==null) {
     		ZAlert.serviceLogicalError("登录超时");
     	}
     	String role=su.getRole();
     	if(BizConst.ROLE.R_USER.equals(role)) {
     		params.put("practitionerId", su.getUserId());
     	}
     	
         exportServiceImpl.doExportPojectNew(params,response.getOutputStream());
         } catch (Exception e) {
             e.printStackTrace();
         }
	}
    @RequestMapping(value="/loadPercentageDataSet", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object loadPercentageDataSet(HttpServletRequest httpRequest,@RequestParam Map<String,Object> params){
    	
    	SessionUser su= (SessionUser) httpRequest.getSession().getAttribute(BizConst.SESSION_USER);
    	if(su==null) {
    		ZAlert.serviceLogicalError("登录超时");
    	}
    	String role=su.getRole();
    	if(BizConst.ROLE.R_USER.equals(role)) {
    		params.put("creator", su.getAccount());
    	}
	    DataSet dataSet=  percentageService.loadPercentageDataSet(params);
	    return dataSet;
    }
	
    @RequestMapping(value="/addPercentage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public  @ResponseBody Object addPercentage(HttpServletRequest httpRequest,Percentage percentage){
    	
    	SessionUser su= (SessionUser) httpRequest.getSession().getAttribute(BizConst.SESSION_USER);
    	if(su==null) {
    		ZAlert.serviceLogicalError("登录超时");
    	}
    	String role=su.getRole();
    	String account=su.getAccount();
		percentageService.createPercentage(percentage,account,role);
    	return ajaxDoneSuccess("数据操作成功 ");
    }
    
    @RequestMapping(value="/updatePercentage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object updatePercentage(HttpServletRequest httpRequest,Percentage percentage){
    	
    	SessionUser su= (SessionUser) httpRequest.getSession().getAttribute(BizConst.SESSION_USER);
    	if(su==null) {
    		ZAlert.serviceLogicalError("登录超时");
    	}
    	String role=su.getRole();
    	
		percentageService.updatePercentage(percentage,role);
		return ajaxDoneSuccess("数据操作成功 ");
    }
    
	
    @RequestMapping(value="/loadPercentage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object loadPercentage(Long id){
		Optional<Percentage> ops = percentageService.loadPercentageById(id);
		return ajaxQuery("percentage",ops);
    }
	

	
    @RequestMapping(value="/deletPercentage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object deletPercentage(Long id){
		percentageService.deletPercentage(id);
		return ajaxDoneSuccess("数据操作成功 ");
    }
    
    @RequestMapping(value="/loadNextCaseNum", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
   	public @ResponseBody  Object loadNextCaseNum(String today) {
    	if(ZStrUtil.hasNoText(today)){
    		today=ZDateUtil.defaultDate(new Date());
    	}
    	String nextCaseNum=percentageService.loadNewCaseNum(today);
       	return ajaxQuery("nextCaseNum", nextCaseNum);
   	}
    @RequestMapping(value="/updateCaseStatus", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody  Object updateCaseStatus(String type,@ListParam(Long.class)List<Long> ids,Integer  caseStatus){
    	percentageService.updateCaseStatus(type, ids, caseStatus);
		return ajaxDoneSuccess("数据操作成功 ");
    }
    

    

	
}